#version 460


#include "project_matrix.glsl"
#include "vertOut.glsl"



layout(lines_adjacency) in;
layout(triangle_strip, max_vertices=6) out;
//layout(line_strip, max_vertices=7) out;


layout(location = 0) in vec4 gColor[];
layout(location = 1) in vec2 vWidget[];
layout(location = 2) in vec2 vNormal[];
layout(location = 3) flat in uint vInstanceIndex[];

in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];





//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};
void main(void){
	uint InstanceI = vInstanceIndex[0];
	mat3x2 mat = m_LP_SD[InstanceI];
	

	mat2 rMat;
	rMat[0][0] = cos(1.5707963267);
	rMat[0][1] = sin(1.5707963267);

	rMat[1][0] = -sin(1.5707963267);
	rMat[1][1] = cos(1.5707963267);

	/*rMat[0][0] = cos(1.5707963267);
	rMat[0][1] = -sin(1.5707963267);

	rMat[1][0] = sin(1.5707963267);
	rMat[1][1] = cos(1.5707963267);*/
	vec4 v = vec4(0.0, 0.0, 0.0, 1.0);
	v.z = mat[2].y;


	//vec2 v1 = normalize(gl_in[1].gl_Position.xy - gl_in[0].gl_Position.xy);
	//vec2 v2 = normalize(gl_in[1].gl_Position.xy - gl_in[2].gl_Position.xy);
	//vec2 offset = vec2(v1+v2)*10 + 10;
	vec4 PreviousVert;

	vec2 vs[4];
	//vec2 v[4] = {vec2(0),vec2(0),vec2(0),vec2(0)};
	for(uint i=0; i<4; ++i){
		vs[i] = (gl_in[i].gl_Position.xy * mat[2].x) + (mat[1] * vWidget[i]);

		vs[i] += mat[0];
		vs[i] *= m_Scale;
		vs[i] += m_Loc;
		
		//vs[i] = (mat_ProjecView * vec4(vs[i], 0, 1)).xy;
	}
	//vec2 n1 = normalize((normalize(vs[0]) - normalize(vs[1])) - (normalize(vs[2]) - normalize(vs[1])));
	//vec2 n2 = normalize((normalize(vs[1]) - normalize(vs[2])) - (normalize(vs[3]) - normalize(vs[2])));

	vec3 nor0 = cross(vec3((vs[1]) - (vs[0]), 0), vec3(0, 0, 1));
	vec3 nor1 = cross(vec3((vs[2]) - (vs[1]), 0), vec3(0, 0, 1));

	vec3 nor2 = cross(vec3((vs[2]) - (vs[1]), 0), vec3(0, 0, 1));
	vec3 nor3 = cross(vec3((vs[3]) - (vs[2]), 0), vec3(0, 0, 1));

	vec2 n1 = normalize(nor0+nor1).xy;
	vec2 n2 = normalize(nor2+nor3).xy;
	

	v.xy = vs[1];
	gl_Position = mat_ProjecView * v;
	vColor = vec4(0,0,0.0, 0.5);
	EmitVertex();

	v.xy = vs[1] - n1*7;
	gl_Position = mat_ProjecView * v;
	vColor = vec4(0,0,0.0,0);
	EmitVertex();

	v.xy = vs[2];
	gl_Position = mat_ProjecView * v;
	vColor = vec4(0,0,0.0,0.5);
	EmitVertex();

	EndPrimitive();




	v.xy = vs[2];
	gl_Position = mat_ProjecView * v;
	vColor = vec4(0,0,0.0, 0.5);
	EmitVertex();
	
	v.xy = vs[1] - n1*7;
	gl_Position = mat_ProjecView * v;
	vColor = vec4(0,0,0.0,0);
	EmitVertex();

	v.xy = vs[2] - n2*7;
	gl_Position = mat_ProjecView * v;
	vColor = vec4(0,0,0.0,0);
	EmitVertex();

	

	EndPrimitive();


	
	return;

	
}




